#pragma once
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#include <unordered_map>
#include <fstream>

#include "../comm/util.hpp"
#include "../comm/log.hpp"


/**************************************
 * 根据题目list文件，将所有题目加载到内存
 * model: 数据交互模块, 对外提供访问数据的接口,
 */
namespace ns_model
{
    using namespace ns_util;
    using namespace ns_log;

    struct Question
    {
        std::string id;         // 题目编号
        std::string title;      // 题目标题
        std::string star;       // 题目难度
        int cpu_limit;          // 时间限制
        int mem_limit;          // 空间限制
        std::string desc;       // 题目描述
        std::string header;     // 题目预设代码
        std::string tail;       // 题目测试用例, 与header链接形成最终代码
    };
    
    const std::string questions_list = "./questions/questions.list";
    const std::string questions_path = "./questions/";

    class Model
    {
    public:
        Model()
        {
            assert(LoadQuestionsList(questions_list));
        }

        bool LoadQuestionsList(const std::string& questions_list)
        {
            //加载配置⽂件: questions/questions.list + 题⽬编号⽂件
            std::ifstream in(questions_list);
            if(!in.is_open())
            {
                LOG(FATAL) << "加载题库失败, 请检查是否存在题库⽂件\n";
                return false;
            }
            
            std::string line;
            while (getline(in, line))
            {
                std::vector<std::string> tokens;
                StringUtil::SplitString(line, &tokens, " ");
                // 1 判断回文数 简单 1 64
                // 不完整的题目
                if(tokens.size() != 5)  
                {
                    LOG(WARNING) << "加载部分题目失败, 请检查文件格式\n";
                    continue;
                }
                Question question;
                question.id = tokens[0];
                question.title = tokens[1];
                question.star = tokens[2];
                question.cpu_limit = std::stoi(tokens[3]);
                question.mem_limit = std::stoi(tokens[4]);

                std::string question_id_path = questions_path +  question.id + "/";

                FileUtil::ReadFile(question_id_path + "desc.txt", &(question.desc), true);
                FileUtil::ReadFile(question_id_path + "header.cpp", &(question.header), true);
                FileUtil::ReadFile(question_id_path + "tail.cpp", &(question.tail), true);

                questions.emplace(question.id, question);
            }  

            in.close();
            LOG(INFO) << "加载题库成功\n";
            return true;
        }

        bool GetAllQuestions(std::vector<Question>* out)
        {
            if(questions.empty())
            {
                LOG(ERROR) << "用户获取题库失败\n";
                return false;
            }
            for(auto& question : questions) 
                out->emplace_back(question.second);
            
            return true;
        }

        bool GetOneQuestion(Question* out, const std::string& id)
        {
            auto iter = questions.find(id);
            if(iter == questions.end())
            {
                LOG(ERROR) << "用户获取题目(" << id << ")失败\n";
                return false;
            }
            *out = iter->second;
            return true;
        }

        ~Model() = default;
    private:
        std::unordered_map<std::string, Question> questions;  
    };
} 